
#import "PushConfig.h"

@implementation PushConfig

//
// apn provider address
//
@synthesize apnProvider														= _apnProvider;

//
// category filter
//
@synthesize categoryFilter													= _categoryFilter;

//
// init from file
//
- (id)initFromFile:(NSString*)filePath
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// load from file
	//
	NSString* error															= nil;
	NSData* fileData														= [NSData dataWithContentsOfFile:filePath];
	NSMutableDictionary* config												= [NSPropertyListSerialization propertyListFromData:fileData mutabilityOption:NSPropertyListMutableContainersAndLeaves format:0 errorDescription:&error];
	self.apnProvider														= [config valueForKey:@"apnProvider"];
	self.categoryFilter														= [config valueForKey:@"categoryFilter"];
	[error release];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.apnProvider														= nil;
	self.categoryFilter														= nil;
	
	[super dealloc];
}

//
// copy
//
- (id)copyWithZone:(NSZone*)zone
{
	PushConfig* newCopy														= [[[self class] allocWithZone:zone] init];
	if(!newCopy)
		return nil;
	
	newCopy.apnProvider														= [[self.apnProvider copyWithZone:zone] autorelease];
	if(self.categoryFilter)
	{
		NSData* tempData													= [NSKeyedArchiver archivedDataWithRootObject:self.categoryFilter];
		newCopy.categoryFilter												= [NSKeyedUnarchiver unarchiveObjectWithData:tempData];
	}
	
	return newCopy;
}

//
// save to file
//
- (void)saveToFile:(NSString*)filePath
{
	NSMutableDictionary* config												= [NSMutableDictionary dictionaryWithCapacity:2];
	if(self.apnProvider.length)
		[config setValue:self.apnProvider forKey:@"apnProvider"];
	if(self.categoryFilter.count)
		[config setValue:self.categoryFilter forKey:@"categoryFilter"];
	
	[config writeToFile:filePath atomically:YES];
}

@end
